package com.nike.amoeba.ui.frg

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.adapter.TaskStockAdapter
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.db.TaskStockBean
import com.nike.amoeba.databinding.FStockOrderBinding
import com.nike.amoeba.ext.ORDER_TYPE
import com.nike.amoeba.ext.setNbOnItemClickListener
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.TaskModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author nike
 * @Date 2023/7/7 11:59
 * @Description 仓库盘点
 */
class TaskStockFrg : BaseFragment<TaskModel, FStockOrderBinding>(){

    val adapter: TaskStockAdapter by lazy { TaskStockAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    val taskModel: TaskModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.title.set(it.getString("title"))
            mViewModel.code.set(it.getString("code"))
        }
        setToolBarTitle(mViewModel.title.get())
        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            mViewModel.onStockTakeList(mViewModel.code.get())
        }

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }

        mDatabind.swipeRefresh.isEnabled = false
        mDatabind.swipeRefresh.setOnRefreshListener {
            mViewModel.onStockTakeList(mViewModel.code.get())
        }
        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = mFilterList[position] as TaskStockBean
                UIHelper.startTaskStockListFrg(nav(), bean.OrderNo!!, mViewModel.code.get())
            }
        }

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                if (mViewModel.code.get() != null){
                    setToolBarTitle(mViewModel.title.get())
                    mViewModel.onStockTakeList(mViewModel.code.get())
                }
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listTaskStockBean.observe(viewLifecycleOwner, Observer {
            mDatabind.swipeRefresh.isRefreshing = false
            loadsir.showSuccess()
            adapter.setList(it)
            adapter!!.appendList(it)
        })
        eventViewModel.stockListUploadEvent.observeInFragment(this, Observer {
            mDatabind.swipeRefresh.isRefreshing = true
            mViewModel.title.set(it.Title)
            mViewModel.code.set(it.Code)
            mViewModel.onStockTakeList(mViewModel.code.get())
            setToolBarTitle(mViewModel.title.get())
        })
        eventViewModel.zkingType.observeInFragment(this, Observer {dabaBean ->
            if (dabaBean.Type == ORDER_TYPE){
                val bean = adapter.mFilterList.firstOrNull { it.OrderNo == dabaBean.text }
                bean?.let { UIHelper.startTaskStockListFrg(nav(), it.OrderNo!!, mViewModel.code.get()) }
            }
        })
        //搜索
        taskModel.orderSerch.observe(viewLifecycleOwner, {
            adapter!!.filter.filter(it)
        })
    }

}